<?php

namespace C\S\Coupon;

use C\L\Service;

class Coupons extends Service
{

    protected function setModel()
    {
        $this->model = new \C\M\Coupons();
    }

    public function getStatusConfig()
    {
        return [
            'status' => [
                'N' => '未开始',
                'Y' => '已发放',
                'D' => '已结束',
            ]
        ];
    }

    /**
     * 创建代金券
     * coupon_value     int     面额：单位 元
     * use_condition    int     使用条件：满多少元可用
     * effective_time   int     几日内可用：天
     */
    public function createVoucher($add)
    {
        if (!isset($add['coupon_value'])) {
            throw new \Exception('面额不能为空');
        }
        if (!isset($add['use_condition'])) {
            throw new \Exception('使用条件不能为空');
        }
        if (empty($add['effective_time'])) {
            throw new \Exception("几日内可用不能为空");
        }
        $add['type'] = '2';
        $add['coupon_type'] = '1';
        if (!isset($add['status'])) {
            $add['status'] = 'Y';
        }
        $add['payment_amount'] = '1';
        $add['use_condition_msg'] = sprintf('满%s元可用', $add['use_condition']);
        return $this->save($add);
    }

    /**
     * 优惠券核销数+1
     */
    public function verifiNum($coupon_id, $num = 1)
    {
        $verifi_num = $this->getValue('verifi_num', ['id' => $coupon_id]);
        $update = [];
        $update['verifi_num'] = $verifi_num + $num;
        return $this->update($coupon_id, $update);
    }

    /**
     * 通过商品ID查，该商品是否有配置优惠券
     */
    public function goodsCoupon($goods_id)
    {
        $time = time();
        $coupon_info = $this->search([
            'type' => '1',
            'goods_id' => $goods_id,
            'stime' => $time,
            'etime' => $time,
        ], ['stime' => '<=', 'etime' => '>=']);
        return $coupon_info;
    }

    /**
     * 创建vip代金券
     * coupon_value     int     面额：单位 元
     * use_condition    int     使用条件：满多少元可用
     * effective_time   int     几日内可用：天
     */
    public function createVipVoucher($add)
    {
        if (!isset($add['coupon_value'])) {
            throw new \Exception('面额不能为空');
        }
        if (!isset($add['use_condition'])) {
            throw new \Exception('使用条件不能为空');
        }
        if (empty($add['effective_time'])) {
            throw new \Exception("几日内可用不能为空");
        }
        if (empty($add['vip_name'])) {
            throw new \Exception('vip_name字段错误');
        }
        $add['type'] = '3';
        $add['coupon_type'] = '1';
        if (!isset($add['status'])) {
            $add['status'] = 'N';
        }
        $add['payment_amount'] = '1';
        $add['use_condition_msg'] = sprintf('满%s元可用', $add['use_condition']);
        if (isset($add['id'])) {
            $id = $add['id'];
            unset($add['id']);
            return $this->update($id, $add);
        }
        return $this->save($add);
    }

    /**
     *检测是否有符合条件的用户
     */
    public function checkConformUser($vip_name)
    {
        $level = $this->di['s_level']->getLevelCopy($vip_name);
        $sCredit = $level['credit'] ?? '';
        $eCredit = $level['next']['credit'] ?? '';
        if (empty($sCredit) && empty($eCredit)) {
            return false;
        }
        $where = ['credit' => $sCredit];
        $whereType = ['credit' => '>'];
        if (!empty($eCredit)) {
            $where['credit'] = [$sCredit, $eCredit - 1];
            $whereType['credit'] = 'between';
        }
        $users = $this->di['s_user']->searchAll($where, $whereType, ['uid', 'credit']);
        return $users;
    }

    public function singleVipVoucher($add)
    {
        if (!isset($add['coupon_value'])) {
            throw new \Exception('面额不能为空');
        }
        if (!isset($add['use_condition'])) {
            throw new \Exception('使用条件不能为空');
        }
        if (empty($add['effective_time'])) {
            throw new \Exception("几日内可用不能为空");
        }
        $add['type'] = '4';
        $add['coupon_type'] = '1';
        if (!isset($add['status'])) {
            $add['status'] = 'Y';
        }
        $add['payment_amount'] = '1';
        $add['use_condition_msg'] = sprintf('满%s元可用', $add['use_condition']);
        if (isset($add['id'])) {
            $id = $add['id'];
            unset($add['id']);
            return $this->update($id, $add);
        }
        return $this->save($add);
    }
}
